package com.peanut.controller;

import com.peanut.common.Result;
import com.peanut.domain.bo.TabCarSeriesBO;
import com.peanut.domain.ov.TabCarAgeOV;
import com.peanut.dto.TabCarAgeDTO;
import com.peanut.server.TabSeriesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("tabSeries")
@Api
public class TabSeriesController {
    private Logger logger = LoggerFactory.getLogger(TabSeriesController.class);
    @Resource
    private TabSeriesService tabSeriesService;

    //查询全部信息
    @ApiOperation("查询全部车系")
    @GetMapping("allTabSeries")
    public Result FindAllTabSeries() {
        try {
            List<TabCarSeriesBO> tabSeriesBOS = tabSeriesService.FindAllTabSeries();
            logger.info("查询了全部", tabSeriesBOS);
            return Result.OK().setData(tabSeriesBOS);
        } catch (Exception e) {
            logger.error("查询出错", e);
            return Result.ERROR();
        }
    }

    @ApiOperation("模糊查询车系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carSeriesName", value = "车系名称", paramType = "path", dataTypeClass = String.class),
    }
    )
    @PostMapping("findTabSeriesByName/{carSeriesName}")
    public Result findTabSeriesByName(@PathVariable("carSeriesName") String carSeriesName) {
        try {
            List<TabCarSeriesBO> tabSeriesBOS = tabSeriesService.findTabSeriesByName(carSeriesName);
            logger.info("查询了", tabSeriesBOS);

            return Result.OK().setData(tabSeriesBOS);
        } catch (Exception e) {
            logger.error("查询出错", e);
            return Result.ERROR();
        }
    }

    //车辆类型
    @ApiOperation("增加车系类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "TabSeriesVO", value = "车系实体类", paramType = "path", dataTypeClass = String.class),
    }
    )
    @PostMapping("addTabSeries")
    public Result addTabSeries(@RequestBody TabCarAgeOV tabCarAgeOV) {
        try {
            TabCarAgeDTO tabSeriesBO = new TabCarAgeDTO();
            BeanUtils.copyProperties(tabSeriesBO, tabCarAgeOV);
            tabSeriesBO.setSeriesId(Long.parseLong(tabCarAgeOV.getCarAgeName()));
            System.out.println(tabSeriesBO);
            logger.info("添加了", tabSeriesBO);
            tabSeriesService.addTabSeries(tabSeriesBO);
            return Result.OK();
        } catch (Exception e) {
            logger.error("查询出错", e);
            return Result.ERROR();
        }
    }

    //车辆类型
    @ApiOperation("批量车系类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "多个id", paramType = "path", dataTypeClass = String.class),
    }
    )
    @PostMapping("deleteBathByIdsTabSeries/{ids}")
    public Result deleteBathByIdsTabSeries(@PathVariable("ids") String ids) {
        try {
            List<Long> list = new ArrayList<>();
            String[] split = ids.split(",");
            for (String s : split) {
                list.add(Long.parseLong(s));
            }
            logger.info("删除了", ids);
            tabSeriesService.deleteBathByIdsTabSeries(list);
            return Result.OK();
        } catch (Exception e) {
            logger.error("删除出错", e);
            return Result.ERROR();
        }
    }
}
